package com.codedef.controller;

import com.codedef.common.response.Result;
import com.codedef.common.response.ResultCode;
import com.codedef.entity.ReservationRecord;
import com.codedef.service.ReservationRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/reservation")
public class ReservationRecordController {

    @Autowired
    ReservationRecordService reservationRecordService;



    /**
     * 1.获取一个座位，当天的预约情况 In:Date  Out:List
     * 2.获取一个座位，在某一时段是否可预约 In:Date Date  Out:Boolean
     * 3.
     */
    public void getSeatState() {

    }

    @GetMapping("/test")
    public String test() {
        return "Test111";
    }

    /**
     * 预约一个座位
     */
    @PostMapping("/reserve")
    public Result seatReserve(@RequestBody ReservationRecord reservationRecord, @RequestHeader("USER_ID") String userId) {

        if (reservationRecordService.seatReserve(reservationRecord, userId)) {
            return Result.SUCCESS("预约成功");
        }

        return Result.FAIL("预约失败,其他错误");
    }

    /**
     * 查询某个人的预约列表
     */
    @GetMapping("/myReservationList")
    public Result getReservationListByUserId(@RequestHeader("USER_ID") String headerId) {
        int userId = Integer.parseInt(headerId);
        return Result.SUCCESS(reservationRecordService.getReservationListByUserId(userId));
    }

    /**
     * 获取一个座位某天的预约列表
     */
    @PostMapping("/somedayReservationList")
    public Result getReservationListByDateAndSeatId(@RequestBody Map<String, Object> params) {
        if (StringUtils.isEmpty((String) params.get("date")) || !params.containsKey("seatId")) {
            return new Result(ResultCode.PARAM_IS_INVALID);
        }
        log.info("参数是：" + params);
        // 读取参数
        String date = (String) params.get("date");
        int seatId = (int) params.get("seatId");
        // 调用方法查询
        List<ReservationRecord> reservationList = reservationRecordService.getReservationListByDateAndSeatId(date, seatId);
        log.info("查出来的预约记录列表：" + reservationList);
        return Result.SUCCESS(reservationList);
    }

    /**
     * 取消一个预约
     */
    @GetMapping("/cancelReserve/{reservationId}")
    public Result cancelReserve(@PathVariable("reservationId") Integer reservationId, @RequestHeader("USER_ID") Integer userId) {
        int result = reservationRecordService.cancelReserve(reservationId, userId);
        if (result > 0)
            return Result.SUCCESS();
        return Result.FAIL();
    }

    /* --------------------------------- Feign接口 --------------------------------- */
    /**
     * 查询某个人当前可以签到的预约  Feign接口
     */
    @GetMapping("/f/canSignInRecord")
    public ReservationRecord getCanSignInReservationRecordFeign(@RequestParam("seatId") Integer seatId, @RequestParam("userId") Integer userId) {
        System.out.println("OpenFeign接口getCanSignInReservationRecordFeign被访问");
        return reservationRecordService.getCanSignInReservationRecord(userId, seatId);
    }

    /**
     * 更新一个预约记录的状态  Feign接口
     */
    @GetMapping("/f/updateReservationStateByIdFeign")
    public Boolean updateReservationStateByIdFeign(@RequestParam("reservationRecordId") Integer reservationRecordId, @RequestParam("state") Integer state) {
        return reservationRecordService.updateReservationStateById(reservationRecordId, state);
    }

}
